﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.DataVisualization.Charting;

namespace WebApplication.Controllers
{
    public class AnnController : Controller
    {
        Models.DataClasses1DataContext d = new Models.DataClasses1DataContext();

        // GET: Ann
        public ActionResult Index()
        {
            return View(d.Ann);
        }

  

        // GET: Ann/Create
        public ActionResult Create(int id)
        {
            var temp = d.Temp.First(t => t.id == id);
            Models.Ann ann = new Models.Ann()
            {
                dateTime = DateTime.Now,
                idDataSet = temp.Work.idDataSet,
                idWork = temp.idWork,
                test = "",
                testValues = "",
                name = "work: " + temp.Work.name + " step:" + temp.stap,
                note = temp.test,
                data=temp.data



            };
            d.Ann.InsertOnSubmit(ann);
            d.SubmitChanges();



            return RedirectToAction("Edit/"+ann.id);
        }

       

        // GET: Ann/Edit/5
        public ActionResult Edit(int id)
        {
            return View(d.Ann.First(t=>t.id==id));
        }



        public FileContentResult GetChart(int id)
        {
            List<PointF> points = new List<PointF>();

            var ann = d.Ann.First(t => t.id == id);

            if (!string.IsNullOrEmpty(ann.testValues))

                foreach (var p in ann.testValues.Split(';'))
                {
                    try
                    {
                        points.Add(new PointF((float) Convert.ToDouble(p.Split(' ')[0]),
                            (float) Convert.ToDouble(p.Split(' ')[1])));
                    }
                    catch { }
                }
            var chart = new Chart();
            chart.Width = 1000;
            chart.Height = 500;
            chart.BackColor = Color.White;
            chart.BorderlineDashStyle = ChartDashStyle.NotSet;
            chart.BackSecondaryColor = Color.White;
            chart.BackGradientStyle = GradientStyle.TopBottom;
            chart.BorderlineWidth = 0;

            chart.RenderType = RenderType.BinaryStreaming;
            chart.BorderSkin.SkinStyle = BorderSkinStyle.None;
            chart.AntiAliasing = AntiAliasingStyles.All;
            chart.TextAntiAliasingQuality = TextAntiAliasingQuality.Normal;

            chart.Series.Add(CreateSeries(points, SeriesChartType.Line, Color.Red));
            chart.ChartAreas.Add(CreateChartArea());

            var ms = new MemoryStream();
            chart.SaveImage(ms);
            return File(ms.GetBuffer(), @"image/png");
        }


        [NonAction]
        public Series CreateSeries(List<PointF> results,
       SeriesChartType chartType,
       Color color)
        {
            var seriesDetail = new Series();
            seriesDetail.Name = "Errors";
            seriesDetail.IsValueShownAsLabel = false;
            seriesDetail.Color = color;
            seriesDetail.ChartType = chartType;
            seriesDetail.BorderWidth = 2;
            seriesDetail["DrawingStyle"] = "Cylinder";
            seriesDetail["PieDrawingStyle"] = "SoftEdge";
            DataPoint point;

            foreach (var result in results)
            {
                seriesDetail.Points.AddXY(result.X, result.Y);
            }
            seriesDetail.ChartArea = "Errors";

            return seriesDetail;
        }

        public ChartArea CreateChartArea()
        {
            var chartArea = new ChartArea();
            chartArea.Name = "Errors";
            chartArea.BackColor = Color.Transparent;

            return chartArea;
        }

        // POST: Ann/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {

                var v = d.Ann.First(t => t.id == id);
                v.name = collection.GetValues("name")[0];
                v.note = collection.GetValues("note")[0];

                d.SubmitChanges();


                return RedirectToAction("Index");
            }
            catch
            {
                return View("Index");
            }
        }

        // GET: Ann/Delete/5
        public ActionResult Delete(int id)
        {
            d.Ann.DeleteAllOnSubmit(d.Ann.Where(t => t.id == id));
            d.SubmitChanges();
            return RedirectToAction("Index");
        }

        public FileContentResult GetFile(int id)
        {
            var dat = d.Ann.First(t => t.id == id);
            return File(dat.data.ToArray(), "application/zip", dat.name + ".zip");
        }


    }
}
